/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package codemanager.listeners.action;

import codemanager.CodeManager;
import codemanager.database.dao.impl.ProjectDAOImpl;
import codemanager.database.pojo.Project;
import codemanager.widget.CMNewProDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;

/**
 *
 * @author undefined
 */
public class CMActionListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
       //throw new UnsupportedOperationException("Not supported yet.");
        String command="";
        command = e.getActionCommand();
        //新建菜单项
        if("new".equals(command)){
            CMNewProDialog dialog = new CMNewProDialog((JFrame)(((JMenu)(((JPopupMenu)(((JMenuItem)(e.getSource())).getParent())).getInvoker())).getRootPane().getParent()), true);
            dialog.setVisible(true);
        }
        //退出菜单项
        if("exit".equals(command)){
            System.exit(0);
        }
        //关于菜单项
        if("about".equals(command)){
            String message = "JDK版本：1.7\n作者 JT\n时间：2013年3月\n本软件的所有权利均为作者所有，未经作者本人许可不得用于任何盈利和非盈利的用途！";
            JOptionPane.showMessageDialog(null, message, "关于", JOptionPane.INFORMATION_MESSAGE);
        }
        //tab的关闭按钮
        if("close_tab".equals(command)){
           CodeManager codeManager = (CodeManager)(((JPanel)((JButton)(e.getSource())).getParent()).getTopLevelAncestor());//.removeTabAt(((JTabbedPane)((JButton)(e.getSource())).getParent().getParent()).getSelectedIndex());
           codeManager.getRightTabbedPane().removeTabAt(codeManager.getRightTabbedPane().getSelectedIndex()); 
           if(0==codeManager.getRightTabbedPane().getTabCount()){
           codeManager.getRightTabbedPane().setVisible(false);
           }
        }
        //右键菜单 打开菜单项
        if("open".equals(command)){
            System.out.println(((JMenuItem)(e.getSource())).getTopLevelAncestor());
            /*if(((DefaultMutableTreeNode)((JTree)(e.getSource())).getLastSelectedPathComponent()).isLeaf()){
            TreeNode[] nodes = ((DefaultMutableTreeNode)(((JTree)(e.getSource())).getLastSelectedPathComponent())).getPath();
            //获取文件路径
            StringBuffer path = new StringBuffer();
            for(int i=0;i<nodes.length;i++){
              if(1<i){
                if(2==i){
                    ProjectDAOImpl projectDAOImpl = new ProjectDAOImpl();
                    List<Project> list = projectDAOImpl.queryProjectsByName(nodes[2].toString());
                    path.append(((Project)(list.get(0))).getProURL());
                }else{
                  path.append("\\");
                  path.append(nodes[i].toString());
              }
            }
            }
            CodeManager codeManager = (CodeManager)((JTree)(e.getSource())).getParent().getParent().getParent().getParent().getParent().getParent().getParent();
            codeManager.addTabToRight(path.toString());
            }*/
        }
    } 
    
}
